﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using LinCms.Core.Entities;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
namespace coreapi.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v7")]
    public class BeveragePurchaseController : ControllerBase
    {
        private readonly IFreeSql db;
        public BeveragePurchaseController(IFreeSql _db)
        {
            this.db = _db;
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet("GetData")]
        public object GetData(int currentPage = 1, int pageSize = 10, string? search = "")
        {
            int currentPages = currentPage; // 当前页码
            int pageSizes = pageSize; // 每页显示的条目数

            var query = db.Select<BeveragePurchase, Beverage, EmployeeUser>()
                .LeftJoin((a, b,c) => a.BeverageID == b.BeverageID)
                .LeftJoin((a, b, c) => a.EmployeeID == c.UID)
                .WhereIf(!string.IsNullOrEmpty(search), (a, b, c) => b.BeverageName.Contains(search))
                .ToList((a, b, c) => new { a, b, c });

            int totalCount = query.Count(); // 总条目数
            var list = query.Skip((currentPages - 1) * pageSize).Take(pageSize).ToList();

            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 返回饮料下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDropdownList")]
        public object GetDropdownList()
        {
            var query = db.Select<BeveragePurchase, Beverage, EmployeeUser>()
                         .LeftJoin((a, b, c) => a.BeverageID == b.BeverageID)
                        .LeftJoin((a, b, c) => a.EmployeeID == c.UID)
                          .Where((a, b, c) => a.PurchaseID != null)
                          .ToList((a, b, c) => new { b.BeverageID, b.BeverageName });

            return query;
        }
        /// <summary>
        /// 返回员工下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetHomeList")]
        public object GetHomeList()
        {
            var query = db.Select<BeveragePurchase, Beverage, EmployeeUser>()
                         .LeftJoin((a, b, c) => a.BeverageID == b.BeverageID)
                        .LeftJoin((a, b, c) => a.EmployeeID == c.UID)
                          .Where((a, b, c) => a.PurchaseID != null)
                          .ToList((a, b, c) => new { c.UID, c.UName });

            return query;
        }
        /// <summary>
        /// 添加菜单数据
        /// </summary>
        /// <param name="beverage"></param>
        /// <returns></returns>
        [HttpPost]
        public int Post(int BeverageID, int UID, BeveragePurchase purchase)
        {
            //先添加订单表
            var beveragePurchase = new BeveragePurchase()
            {
                BeverageID = BeverageID,
                EmployeeID = UID,
                PurchaseDate = purchase.PurchaseDate,
                Quantity = purchase.Quantity,
            };
            var affectedRows = db.Insert(beveragePurchase).ExecuteAffrows();
            //计算这个种类的库存数量
            var existingQuantity = db.Select<BeverageInventory>()
                .Where(a => a.BeverageID == BeverageID)
                .ToList()
                .Sum(a => a.Quantity);

            var totalQuantity = existingQuantity + (int)purchase.Quantity;

            var beverage2 = db.Update<BeverageInventory>().Set(a => new BeverageInventory
            {
                Quantity = totalQuantity,
            }).Where(a => a.BeverageID == BeverageID).ExecuteAffrows();

            return affectedRows;
        }
        /// <summary>
        /// 编辑订单数据
        /// </summary>
        /// <param name="PurchaseID"></param>
        /// <param name="BeverageID"></param>
        /// <param name="UID"></param>
        /// <returns></returns>
        [HttpPut]
        public int Put(int PurchaseID ,int BeverageID, int UID)
        {
            var beverage2 = db.Update<BeveragePurchase>().Set(a => new BeveragePurchase
            {
                BeverageID = BeverageID,
                EmployeeID = UID,
            }).Where(a => a.PurchaseID == PurchaseID).ExecuteAffrows();
            return beverage2;
        }
        /// <summary>
        /// 删除订单数据
        /// </summary>
        /// <param name="PurchaseID"></param>
        /// <returns></returns>
        [HttpDelete]
        public int Delete(int PurchaseID)
        {
            var beverage2 = db.Delete<BeveragePurchase>()
                                .Where(a => a.PurchaseID == PurchaseID)
                                .ExecuteAffrows();
            return beverage2;
        }
    }
}
